import { IFeidaoAiBrowserComponent } from '@feidao/web/interfaces';
import error from '@feidao/web/atom/browser/msgbox/error';
import call_service from '@feidao/web/atom/browser/msg/call-service';
import { spaceid, send_msg as url } from './config';
import render from '@feidao/web/atom/browser/render/render';

import P002 from './P002';
import P003 from './P003';
import set from '@feidao/web/atom/core/local/set';


export default async function (fd: IFeidaoAiBrowserComponent, ...args: any[]) {






	//获取短信验证码
	// const cur_dom = args[0].currentTarget as HTMLDivElement;
	const dom = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-id="npn"]');
	const regex = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
	const value = dom.value;


	if (!value.match(regex)) {
		await error('请输入正确的手机号')
		return;
	}


	set(fd, 'phone', value);

	const param = {
		phone: value,
		spaceid,
		strategyid: '32fce1f7-b1c3-49b0-b191-828214d7fbae',
		systemid: '12'
	};

	const res = await call_service<{ code: number, msg: string }>(url, 'busiservice.flippedclass.sendphoneverifycode', param, false, false);
	if (res.msg === '验证码发送成功') {

		render(fd, {}, P003, 'P002', 'inner');
		const timer_dom = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-id="timer"]')

		let num = 60;

		const t = setInterval(() => {
			timer_dom.innerText = `${num}s`;
			num--
			if (num < 0) {
				clearInterval(t);
				render(fd, {}, P002, 'P002', 'inner');
				return;
			}
		}, 1000)

	} else {

		await error('此手机号接收验证码已超过规定次数');

	}




}

